package com.hyyt.shopjsp.store.info.controller;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.product.pojo.ShopShopProCategory;
import com.hyyt.shopjsp.product.service.IShopShopProCategoryService;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.json.FastJsonUtils;

/**
 * Copyright (c) 2017 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
 * @author Wang Youchun on 2017/04/24 0024下午 13:46.
 */

@Controller
@RequestMapping("/back/selfSupportShopProCategory")
public class SelfSupportShopProCategoryController extends BaseController{
	
	private Logger log = Logger.getLogger(SelfSupportShopProCategoryController.class);
	private String basePath="/back/shop/selfSupportShopProCategory/";
	@Resource
	private IShopShopProCategoryService shopShopProCategoryService;//店铺内部商品分类service
	
	/**
	 * 跳转到自营店铺内部分类页面
	 * @param shopInfoId 店铺id
	 * @param param
	 * @return
	 */
	@RequestMapping("gotoSelfSupportShopProCategory")
	public String gotoSelfSupportShopProCategory(String shopInfoId, Map<String, Object> param){
		param.put("shopInfoId", shopInfoId);
		return basePath+"selfSupportShopProCategoryList";
	}
	
	/**
	 * 根据店铺ID查询该店铺的商品
	 * @param page
	 * @param shopProCategoryName	分类名称
	 * @param isShow				是否显示：	0：未显示、1：已显示
	 * @param shopInfoId			店铺ID
	 */
	@RequestMapping("getSelfSupportShopProCategoryList")
	@ResponseBody
	public void getSelfSupportShopProCategoryList(String shopInfoId,String shopProCategoryName,String isShow,Page page, HttpServletResponse response, HttpServletRequest request){
		Map<String, Object> param = new HashMap<>();
		if(Utils.objectIsNotEmpty(shopProCategoryName)){
			param.put("shopProCategoryName", shopProCategoryName.trim());
		}
		if(Utils.objectIsNotEmpty(isShow)){
			param.put("isShow", isShow);
		}
		param.put("shopInfoId", shopInfoId);
		param.put("sortColumns", "shopProCategoryId desc");
		int totalRecordCount =shopShopProCategoryService.countShopShopProCategory(param);
		page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
		List<ShopShopProCategory> productInfoList = shopShopProCategoryService.queryShopShopProCategory(param, page.getPageRecordBeginIndex(), page.getPageSize());
		param.clear();
		param.put("total", totalRecordCount);// total键 存放总记录数，必须的
		param.put("rows", productInfoList);// rows键 存放每页记录 list
		ResultMsg.create(param, response);  //把内容发送到页面
	}

	/**
	 * 根据店铺内部分类id获得对象
	 * @param shopProCategoryId
	 * @return
	 */
	@RequestMapping(value="getObjectById",method=RequestMethod.POST)
	@ResponseBody
	public ResultMsg getObjectById(String shopProCategoryId){
		if(Utils.objectIsNotEmpty(shopProCategoryId)){
			ShopShopProCategory shopProCategory = shopShopProCategoryService.loadShopShopProCategory(Integer.parseInt(shopProCategoryId));
			return ResultMsg.create(true, null, FastJsonUtils.toJSONString(shopProCategory));
		}
		return ResultMsg.create(false);
	}

	/**
	 * 保存或修改店铺内部分类
	 * @param shopProCategory
	 * @return
	 */
	@RequestMapping(value="saveOrUpdateShopPrCategory",method=RequestMethod.POST)
	@ResponseBody
	public ResultMsg saveOrUpdateShopPrCategory(ShopShopProCategory shopProCategory){
		boolean flag=false;
		if(Utils.objectIsNotEmpty(shopProCategory)){//判断对象是否为空
			if(Utils.objectIsNotEmpty(shopProCategory.getShopProCategoryId())){//判断主键id不为空
				//修改对象
				shopShopProCategoryService.updateShopShopProCategory(shopProCategory);
				flag=true;
			}else{//插入对象
				shopProCategory.setCreateTime(new Timestamp(System.currentTimeMillis()));
				shopProCategory.setLevel(1);
				shopProCategory.setLoadType("1");
				shopProCategory.setParentId(1);
				shopShopProCategoryService.saveShopShopProCategory(shopProCategory);
				flag=true;
			}
		}
		return ResultMsg.create(flag);
	}

	/**
	 * 根据传入的ids进行删除操作
	 * @param shopProCategoryId
	 * @return
	 */
	@RequestMapping(value="deleteSelfSupportShopProCategory",method=RequestMethod.POST)
	@ResponseBody
	public ResultMsg deleteSelfSupportShopProCategory(String shopProCategoryId){
		boolean flag=false;
		if(Utils.objectIsNotEmpty(shopProCategoryId)){
			shopShopProCategoryService.deleteShopShopProCategory(Integer.parseInt(shopProCategoryId));
			flag=true;
		}
		return ResultMsg.create(flag);
	}
}
